# class base for controller
from google.appengine.ext import webapp
# needed for display
from tools import picture_manager

class Index(webapp.RequestHandler):
    '''
    '''

    def get(self):
        '''
        '''
        # get the language id
        locale_id = self.request.get('language')
        # get the size name
        size = self.request.get('size')

        # get the size name
        nb_of_pictures = self.request.get('nb_of_pictures')

        #
        pi_manager = picture_manager.PictureManager(
                locale_id=locale_id, size=size)

        # display the picture to show
        template = pi_manager.show(nb_of_pictures=nb_of_pictures)
        return self.response.out.write(template)

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        # retrieve the offset
        offset = self.request.get('offset')

        # retrieve the author_key
        author_key = self.request.get('author_key')
        if author_key == '':
            author_key = None

        # retrieve the collection_key
        collection_key = self.request.get('collection_key')
        if collection_key == '':
            collection_key = None

        # retrieve the exposition_key
        exposition_key = self.request.get('exposition_key')
        if exposition_key == '':
            exposition_key = None

        # retrieve the size
        nb_of_pictures = self.request.get('nb_of_pictures')

        # display the author to show
        template = pi_manager.show(
                    nb_of_pictures=nb_of_pictures,
                    offset=offset,
                    author_key=author_key,
                    collection_key=collection_key,
                    exposition_key=exposition_key)
        return self.response.out.write(template)

class RSS(webapp.RequestHandler):
    '''
    '''

    def get(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        # retrieve the author_key
        author_key = self.request.get('author_key')
        if author_key == '':
            author_key = None

        # retrieve the collection_key
        collection_key = self.request.get('collection_key')
        if collection_key == '':
            collection_key = None

        # retrieve the exposition_key
        exposition_key = self.request.get('exposition_key')
        if exposition_key == '':
            exposition_key = None

        # display the author to show
        template = pi_manager.rss(
                    author_key=author_key,
                    collection_key=collection_key,
                    exposition_key=exposition_key)
        return self.response.out.write(template)

class Author(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        # retrieve the page
        picture_key = self.request.get('picture_key')

        # retrieve the author key for update
        author_key = self.request.get('select_author_key')

        # small check
        if not author_key == '':
            pi_manager.author(picture_key, author_key)

        # get the size name
        nb_of_pictures = self.request.get('nb_of_pictures')

        # display the page to edit
        template = pi_manager.edit(picture_key, nb_of_pictures=nb_of_pictures)
        return self.response.out.write(template)

class Collection(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        # retrieve the page
        picture_key = self.request.get('picture_key')

        # retrieve the collection key for update
        collection_key = self.request.get('select_collection_key')

        # small check
        if not collection_key == '':
            pi_manager.collection(picture_key, collection_key)

        # get the size name
        nb_of_pictures = self.request.get('nb_of_pictures')

        # display the page to edit
        template = pi_manager.edit(picture_key, nb_of_pictures=nb_of_pictures)
        return self.response.out.write(template)

class Exposition(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        # retrieve the page
        picture_key = self.request.get('picture_key')

        # retrieve the exposition key for update
        exposition_key = self.request.get('select_exposition_key')

        # small check
        if not exposition_key == '':
            pi_manager.exposition(picture_key, exposition_key)

        # retrieve the size
        nb_of_pictures = self.request.get('nb_of_pictures')

        # display the page to edit
        template = pi_manager.edit(picture_key, nb_of_pictures=nb_of_pictures)
        return self.response.out.write(template)

class New(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        #
        picture_key = pi_manager.new()

        # retrieve the size
        nb_of_pictures = self.request.get('nb_of_pictures')

        # display the picture to edit
        template = pi_manager.edit(picture_key, nb_of_pictures=nb_of_pictures)
        return self.response.out.write(template)

class Edit(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        # get title
        picture_key = self.request.get('picture_key')

        # retrieve the size
        nb_of_pictures = self.request.get('nb_of_pictures')

        # display the picture to edit
        template = pi_manager.edit(picture_key, nb_of_pictures=nb_of_pictures)
        return self.response.out.write(template)

class Save(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        # retrieve the picture title of the category
        picture_key = self.request.get('picture_key')

        # retrieve the size
        nb_of_pictures = self.request.get('nb_of_pictures')

        # delete
        if not self.request.get('delete') == '':
            # tools delete the picture
            pi_manager.delete(picture_key)

            # back to the index
            return self.redirect('/picture?language=%s&size=%s' % (
                pi_manager.language.locale_id,
                pi_manager.size.width))

        # cancel
        elif not self.request.get('new') == '':
            #
            picture_key = pi_manager.new()

        # cancel
        elif not self.request.get('cancel') == '':
            # display the picture gallery
            template = pi_manager.show(nb_of_pictures=nb_of_pictures)
            return self.response.out.write(template)

        # we save and display the picture
        else:
            # get title
            title = self.request.get('title')

            # get info
            info = self.request.get('info')

            # tools save the picture
            pi_manager.save(
                    picture_key, title, info)

        # display the picture to edit
        template = pi_manager.edit(picture_key, nb_of_pictures=nb_of_pictures)
        return self.response.out.write(template)

class EditImage(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        # retrieve the page
        picture_key = self.request.get('picture_key')

        # retrieve the size
        nb_of_pictures = self.request.get('nb_of_pictures')

        # retrieve the offset
        offset = self.request.get('offset')

        # get title
        offset = self.request.get('offset')

        # retrieve the page
        album = self.request.get('album')

        # display the page to edit
        template = pi_manager.edit_image(
                picture_key,
                page='picture',
                offset=offset,
                album=album)
        return self.response.out.write(template)

class SaveImage(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        # retrieve the page
        picture_key = self.request.get('picture_key')

        # retrieve the image
        url = self.request.get('url')

        # retrieve the image
        width = self.request.get('width')

        # retrieve the image
        height = self.request.get('height')

        # save first
        pi_manager.save_picture_image(picture_key, url)

        # retrieve the size
        nb_of_pictures = self.request.get('nb_of_pictures')

        # back to the picture currently edited
        template = pi_manager.edit(picture_key, nb_of_pictures=nb_of_pictures)
        return self.response.out.write(template)

class SelectAlbum(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')

        #
        pi_manager = picture_manager.PictureManager(
                language_key=language_key, size_key=size_key)

        # retrieve the page
        picture_key = self.request.get('picture_key')

        # retrieve the page
        album = self.request.get('album')

        # retrieve the size
        nb_of_pictures = self.request.get('nb_of_pictures')

        # display the page to edit
        template = pi_manager.edit_image(
                picture_key,
                album=album,
                nb_of_pictures=nb_of_pictures)
        return self.response.out.write(template)
